#include <REGX52.H>

sbit LED = P3 ^ 1;
sbit BTN = P3 ^ 7;

void delay_ms(unsigned int ms)
{
    unsigned int i, j;
    for (i = 0; i < ms; i++)
    {
        for (j = 0; j < 123; j++)
            ;
    }
}

void main()
{
    unsigned char state = 0; 
    unsigned int blink_timer = 0;
    bit btn_pressed = 0;
    
    LED = 1; 

    while(1)
    {
        // 按键检测（带消抖）
        if(BTN == 0 && !btn_pressed)
        {
            delay_ms(10);
            if(BTN == 0)
            {
                btn_pressed = 1;
                state = (state + 1) % 3; // 切换状态
                while(BTN == 0); // 等待按键释放
                delay_ms(10);
            }
        }
        else if(BTN == 1)
        {
            btn_pressed = 0;
        }

        // 状态处理
        switch(state)
        {
            case 0: // 常亮
                LED = 1;
                break;
                
            case 1: // 闪烁
                blink_timer++;
                if(blink_timer >= 300) 
                {
                    blink_timer = 0;
                    LED = ~LED;
                }
                break;
                
            case 2: // 熄灭
                LED = 0;
                break;
        }
        
        delay_ms(1); // 主循环延时
    }
}